package cn.com.cym.gamezao.jwt;

import cn.com.cym.gamezao.base.ResultResponse;
import cn.com.cym.gamezao.entity.authorize.User;
import cn.com.cym.gamezao.exception.BaseException;

/**
 * JWT 获取的用户
 */
public class RequestUser {

    private final static ThreadLocal<User> requestUser=new ThreadLocal<>();

    /**
     * 当前线程的保存用户
     * @param user user
     */
    public static void add(User user) {
        requestUser.set(user);
    }

    /**
     * 获取当前的用户
     * @return User
     */
    public static User get() {
        User user = requestUser.get();
        if(user == null ){
            throw new BaseException("用户未登录",new ResultResponse("用户未登录",403));
        }
        return user;
    }

    /**
     * 过滤null
     * @return
     */
    public static User getOne(){
        User user = requestUser.get();
        return user;
    }

    /**
     * 清除用户
     */
    public static void remove() {
        requestUser.remove();
    }
}
